/**
 * ubus test
 * 2024/2/26
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <libubox/blobmsg_json.h>
#include <libubox/avl.h>
#include <libubus.h>
#include <sys/stat.h>

#define ROOT_PATH 0
struct ubus_context *ubus_ctx;
static const struct blobmsg_policy execute_policy[] = {
    [ROOT_PATH] = {.name="path",    .type = BLOBMSG_TYPE_STRING},
    [1] = {},
};

static int obj_excute(struct ubus_context *ctx,struct ubus_object *obj,
                      struct ubus_request_data *req,    const char *method,
                      struct blob_attr *msg);
            
static struct ubus_method obj_methods[]={
    UBUS_METHOD("excute",obj_excute,execute_policy),
    UBUS_METHOD("h",obj_excute,execute_policy+1),
};
static struct ubus_object_type obj_type = UBUS_OBJECT_TYPE("u1",obj_methods); 
static struct ubus_object obj = {
    .name = "helloworld",
    .type = &obj_type,
    .methods = obj_methods,
    .n_methods = ARRAY_SIZE(obj_methods),
};
int main()
{
    int ret;
    uloop_init();
    signal(SIGPIPE, SIG_IGN);

    ubus_ctx = ubus_connect(NULL);
    ubus_add_uloop(ubus_ctx);
    ret = ubus_add_object(ubus_ctx,&obj);
    if(ret)
    {
         fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
    }
    else
    {
        uloop_run();
    }
    ubus_free(ubus_ctx);
    uloop_done();
    return 0;
    return 0;
}
static int obj_excute(struct ubus_context *ctx,struct ubus_object *obj,
                      struct ubus_request_data *req,    const char *method,
                      struct blob_attr *msg)
{
    printf("u1 call hello world");
    return 0;
}